package com.example.booking.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.booking.domain.Patient;
import com.example.booking.mapper.PatientMapper;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

/**
 *就诊人控制器：接受请求，执行响应
 *
 * @author 植轶
 *
 */
//跨域访问
@CrossOrigin
@RestController
//@RequestMapping("/api/patient")
public class PatientController {
	
	//需要
	//spring 容器；依赖注入(程序运行时自动创建相关对象，自动初始化)
	//容器负责给mapper赋值
	//自动注入
	@Autowired
	PatientMapper mapper;
	
	//GET       /api/patient
	@GetMapping("/api/patient")
	public List<Patient> list(){
		return mapper.findAll();
	}
	
	//GET       /api/patient/23
	@GetMapping("/api/patient/{id}")
	public Patient load(@PathVariable int id){
		return mapper.findById(id);
	}
	
	//POST      /api/patient
	//1)表单提交，参数不用加@RequestBody，默认为  @RequestParam
	//2)提交JAON 格式数据，参数必须加@RequestBody
	//前端使用了axios 发送了JSON格式数据，数据位于，HTTP请求的负载中
	@PostMapping("/api/patient")
	public Patient save( @RequestBody Patient patient) {
		mapper.create(patient);
		return patient;
	}
	
	//PUT       /api/patient/id
	@PutMapping("/api/patient/{id}")
	public Patient updata(@PathVariable int id,@RequestBody Patient patient) {
		//设置就诊人的编号
		patient.setId(id);
		mapper.updata(patient);
		return patient;
	}
	
	//DELETE    /api/patient/id
	@DeleteMapping("/api/patient/{id}")
	public ResponseEntity<String> remove(@PathVariable int id) {
		mapper.delete(id);
		//1.协议实体  body
		//2.协议头    header
		//3.协议状态码  status
		return new ResponseEntity<>(HttpStatus.OK);
	}
	

}
